package com.ruoyi.locksApi.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ruoyi.common.annotation.Anonymous;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;


@RestController
@Slf4j
@Anonymous
@ConditionalOnProperty(value = "spring.profiles.active", havingValue = "center", matchIfMissing = false)
public class LockCallbackController {

    @Resource
    KafkaTemplate<String, String> kafkaTemplate;
    @RequestMapping(value = "/lock/callback", method = {RequestMethod.POST, RequestMethod.GET})
    public JSONObject receiveCallback(HttpServletRequest request, @RequestBody(required = false) String body) {
        Map<String, Object> params = new HashMap<>();
        // 获取所有URL参数
        Enumeration<String> paramNames = request.getParameterNames();
        while (paramNames.hasMoreElements()) {
            String paramName = paramNames.nextElement();
            params.put(paramName, request.getParameter(paramName));
        }
        // 获取body内容
        if (body != null && !body.isEmpty()) {
            params.put("body", body);
        }
        log.info("收到回调: {}", JSONObject.toJSONString(params, SerializerFeature.PrettyFormat));

       kafkaTemplate.send("liuzhong", JSONObject.toJSONString(params, SerializerFeature.PrettyFormat));


        JSONObject resp = new JSONObject();
        resp.put("result", "ok");
        resp.put("msg", "callback received");
        return resp;
    }
} 